package com.example.xiaomi.Model.Dao;

import com.example.xiaomi.Model.entity.GodsEntity;
import com.example.xiaomi.Model.utils.BaseDB;
import com.example.xiaomi.Model.utils.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;


public class GodsDao extends BaseDB {
    public List<GodsEntity> selectall(){
        String sql = "SELECT sid,gname,gprice,gdetails,ghelves,sname,is_sort,ping  FROM `is_gods`,`is_sort`,`is_pictre` where 1 <=> 1 and is_gods.gid <=>  is_sort.sid and is_pictre.pid <=> is_gods.gid and is_gods.ghelves <=> 1 order by is_sort.sid  desc ";
        return super.query(sql,new GodsRowMapper());
    }
    public List<GodsEntity> specify(int id){
        String sql = "SELECT sid,gname,gprice,gdetails,ghelves,sname,is_sort,ping  FROM `is_gods`,`is_sort`,`is_pictre` where 1 <=> 1 and is_gods.gid <=>  is_sort.sid and is_pictre.pid <=> is_gods.gid and is_gods.ghelves <=> 1 and sid <=>"+id;
        return super.query(sql,new GodsRowMapper());
    }
    class GodsRowMapper implements RowMapper<GodsEntity> {
        @Override
        public GodsEntity rowmap(ResultSet rs) throws SQLException {
            return new GodsEntity(rs.getString("gname"),
                                  rs.getBigDecimal("gprice"),
                                  rs.getString("gdetails"),
                                  rs.getString("ghelves"),
                                  rs.getString("is_sort"),
                                  rs.getInt("sid"),
                                  rs.getString("sname"),
                                  rs.getString("ping"));
        }
    }
}
